package org.ala.tiktools.mybatis.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author ala
 * @date 2025-02-17 13:13
 */
public class TikToolsMyBatisSpringContext implements ApplicationContextAware {

    public static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static <T> T getBeanWithAssert(Class<T> clazz) {
        return context.getBean(clazz);
    }
    public static <T> T getBean(Class<T> clazz) {
        try {
            return context.getBean(clazz);
        } catch (Exception e) {
            return null;
        }
    }
    public static <T> T getBeanOrNew(Class<T> clazz) throws InstantiationException, IllegalAccessException {
        T bean = getBean(clazz);
        if (bean == null) {
            bean = clazz.newInstance();
        }
        return bean;
    }
}
